/* 
 * File:   ClientList.h
 * Author: faraj
 *
 * Created on September 26, 2009, 8:14 AM
 */

#ifndef _CLIENTLIST_H
#define	_CLIENTLIST_H
#define MAX_CLIENTS 10

#include "Client.h"


class ClientList
{
private:   
    List<Client*> clientList;
    
public:
    
    bool setClient(Client *cli)
    {
        if(clientList.Size() > MAX_CLIENTS)
            return false;
        if(checkAlreadyAddressIn(cli))
            return false;
        
        clientList.Insert(clientList.Size(),cli);
        return true;
    }

    Client* getClient(int index)
    {
        return clientList[index];
    }

    bool removeClient(Client *cli)
    {
        for(int i=0;i<clientList.Size();i++)
        {
            if(clientList[i]==cli)
            {
                if(cli->close())
                {
                    clientList.Remove(i);
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        return false;
    }
    
    void removeAll()
    {
        for (int i = 0; i < clientList.Size(); i++) 
        {
            clientList.Remove(0);
        }
    }
    
    int count()
    {
        return clientList.Size();
    }
    
    bool checkAlreadyAddressIn(Client *cli)
    {
        for (int i = 0; i < clientList.Size(); i++) 
        {
            if(*(clientList[i]->getAddress()->getAddress()) 
                    == *(cli->getAddress()->getAddress()))
                return true;
        }
        return false;
    }
};

#endif	/* _CLIENTLIST_H */

